package com.eoe.wiki.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.eoe.wiki.Constants;

import android.os.Environment;
import android.text.TextUtils;

public class FileUtil {

	public static File getExternalStorageDir(){
		return Environment.getExternalStorageDirectory();
	}
	
	public static String getExternalStoragePath(){
		return getExternalStorageDir().getAbsolutePath();
	}
	
	public static String getExternalStorageState(){
		return Environment.getExternalStorageState();
	}
	
	public static boolean isExternalStorageEnable(){
		String state = getExternalStorageState();
		if(Environment.MEDIA_MOUNTED.equals(state)){
			return true;
		}
		return false;
	}
	
	public static void initExternalDir(boolean cleanFile){
		if(isExternalStorageEnable()){
			File external = new File(Constants.EXTERNAL_DIR);
			if(!external.exists()){
				external.mkdirs();
			}
			
			File cache = new File(Constants.CACHE_DIR);
			if(!cache.exists()){
				cache.mkdirs();
			}else{
				if(cleanFile){
					cleanFile(cache,DateUtil.WEEK_MILLIS);
				}
			}
		}
		
	}
	
	private static int cleanFile(File dir,long maxInterval){
		File[] files = dir.listFiles();
		if(files==null){
			return 0;
		}
		int beforeNum = 0;
		long current = System.currentTimeMillis();
		for(File file:files){
			long lastModifiedTime = file.lastModified();
			if(current - lastModifiedTime > maxInterval){
				file.delete();
				beforeNum++;
			}
		}
		return beforeNum;
	}
	
	public String getFileContent(String path){
		File file = new File(path);
		return getFileContent(file);
	}
	
	public static String getFileContent(File file){
		if(!file.exists()){
			return null;
		}
		FileInputStream fis = null; 
		try {
			fis = new FileInputStream(file);
			StringBuilder sb = new StringBuilder();
			byte[] bytes = new byte[1024];
			int length  = 0;
			while((length=fis.read(bytes))!=-1){
				sb.append(new String(bytes,0,length));
			}
			return sb.toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	public static boolean saveFile(String content,String path){
		if(isExternalStorageEnable()){
			File cacheFile = new File(path);
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(cacheFile);
				fos.write(content.getBytes());
				fos.flush();
				return true;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(fos!=null){
					try {
						fos.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return false;
	}
	public static boolean deleteFile(String path){
		if(TextUtils.isEmpty(path)){
			return false;
		}
		File file = new File(path);
		if(file.exists()){
			file.delete();
		}
		return true;
	}
	
	public static boolean isFileExist(String path){
		if(TextUtils.isEmpty(path)){
			return false;
		}
		File file = new File(path);
		return file.exists();
	}
}
